package com.sairobo.base.common.utils;

import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.xslf.usermodel.*;

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class PPT2Image
{

	public static void toImage2007() throws Exception
	{
		FileInputStream is = new FileInputStream("C:\\Users\\76062\\Desktop\\演示文稿1.pptx");
		XMLSlideShow ppt = new XMLSlideShow(is);
		is.close();

		Dimension pgsize = ppt.getPageSize();
		System.out.println(pgsize.width + "--" + pgsize.height);

		for (int i = 0; i < ppt.getSlides().size(); i++)
		{
			try
			{
				// 防止中文乱码
				for (XSLFShape shape : ppt.getSlides().get(i).getShapes())
				{
					if (shape instanceof XSLFTextShape)
					{
						XSLFTextShape tsh = (XSLFTextShape) shape;
						for (XSLFTextParagraph p : tsh)
						{
							for (XSLFTextRun r : p)
							{
								r.setFontFamily("宋体");
							}
						}
					}
				}
				BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
				Graphics2D graphics = img.createGraphics();
				// clear the drawing area
				graphics.setPaint(Color.white);
				graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));

				// render
				ppt.getSlides().get(i).draw(graphics);

				// save the output
				String filename = "C:\\Users\\76062\\Desktop\\demo\\" + (i + 1) + ".jpg";
				System.out.println(filename);
				FileOutputStream out = new FileOutputStream(filename);
				javax.imageio.ImageIO.write(img, "png", out);
				out.close();
			}
			catch (Exception e)
			{
				System.out.println("第" + i + "张ppt转换出错");
			}
		}
		System.out.println("7success");
	}

	public static void toImage2003()
	{
		try
		{
			HSLFSlideShow ppt = new HSLFSlideShow(new HSLFSlideShowImpl("C:\\Users\\76062\\Desktop\\test.pptx"));
			System.out.println(ppt);
			Dimension pgsize = ppt.getPageSize();
			for (int i = 0; i < ppt.getSlides().size(); i++)
			{
				// 防止中文乱码
				for (HSLFShape shape : ppt.getSlides().get(i).getShapes())
				{
					if (shape instanceof HSLFTextShape)
					{
						HSLFTextShape tsh = (HSLFTextShape) shape;
						for (HSLFTextParagraph p : tsh)
						{
							for (HSLFTextRun r : p)
							{
								r.setFontFamily("宋体");
							}
						}
					}
				}
				BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
				Graphics2D graphics = img.createGraphics();
				// clear the drawing area
				graphics.setPaint(Color.white);
				graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));

				// render
				ppt.getSlides().get(i).draw(graphics);

				// save the output
				String filename = "C:\\Users\\76062\\Desktop\\demo\\" + (i + 1) + ".jpg";
				System.out.println(filename);
				FileOutputStream out = new FileOutputStream(filename);
				javax.imageio.ImageIO.write(img, "png", out);
				out.close();
				// resizeImage(filename, filename, width, height);

			}
			System.out.println("3success");
		}
		catch (Exception e)
		{
			// TODO: handle exception
		}
	}
	
	public static void main(String[] args) throws Exception
	{
		System.out.println("1");
//		toImage2003();
		toImage2007();
	}
}
